package com.fingard.dsp.bank.directbank.bob01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 北京银行-余额查询接口新增
 */
public class BOB01Balance extends BOB01Base implements IBalance {
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
    	String balanceCommand = getBankConfig().getSetValue("commandCode","CebankQueryBalanceOp", "7506");
    	WriteBankLogLn2("余额查询调用银行的指令为(commandCode):" + balanceCommand);
    	if ("CebankQueryBalanceOp".equals(balanceCommand)) {
			return cebankQueryBalanceOp(p_req7506);
		} else {
			return cebankQueryAvailableBalanceOp(p_req7506);
		}
    }

	public RetAts7506BalQry cebankQueryBalanceOp(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		retAts.writeStart();
		try {
			BOB01Header bankHeader = new BOB01Header();
			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			//写入通用的头部节点
			writeCommonHeader(xmlWriter);
			String opName = "CebankQueryBalanceOp";
			while (p_req7506.readRecord()) {
				xmlWriter.writeStartElement("opReq");
				xmlWriter.writeElementString("opName", opName);
				xmlWriter.writeElementString("serialNo", getQryTransID());
				xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));
				xmlWriter.writeStartElement("ReqParam");
				xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
				xmlWriter.writeEndElement();
				xmlWriter.writeEndElement();
			}
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), opName);
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();

				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

				if (tmpBkReader != null) {
					RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
					String tmpRetCode = tmpBkReader.getFirstText("/BCCBEBankData/opRep/retCode");
					String tmpRetInfo = tmpBkReader.getFirstText("/BCCBEBankData/opRep/errMsg");
					if (tmpRetCode != null && tmpRetCode.equals("0")) {
						if (tmpBkReader.read("/BCCBEBankData/opRep/opResult")) {
							tmpRetDetail.respCode = "0";
							tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("acctCur"));
							;
							tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("acctName");
							tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balance").replaceAll(",", "");
							tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("curBal").replaceAll(",", "");
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpRetInfo;
					}
					retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
				} else {
					retAts.writeRespCode("-1");
					retAts.writeRespInfo(tmpSbMsg.toString());
				}
			} else {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo(tmpStrRet[1]);
			}
		} catch (Exception e) {
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(e.getMessage());
			e.printStackTrace();
		}
		retAts.writeEnd();
		return retAts;
	}

	public RetAts7506BalQry cebankQueryAvailableBalanceOp(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		retAts.writeStart();

		try {
			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			writeCommonHeader(xmlWriter);

			String opName = "CebankQueryAvailableBalanceOp";
			while (p_req7506.readRecord()) {
				xmlWriter.writeStartElement("opReq");
				xmlWriter.writeElementString("opName", opName);
				xmlWriter.writeElementString("serialNo", getQryTransID());
				xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));
				xmlWriter.writeStartElement("ReqParam");
				xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
				xmlWriter.writeEndElement();
				xmlWriter.writeEndElement();
			}
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), opName);

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
					String tmpRetCode = tmpBkReader.getFirstText("/BCCBEBankData/opRep/retCode");
					String tmpRetInfo = tmpBkReader.getFirstText("/BCCBEBankData/opRep/errMsg");
					if (tmpRetCode != null && tmpRetCode.equals("0")) {
						if (tmpBkReader.read("/BCCBEBankData/opRep/opResult")) {
							tmpRetDetail.respCode = "0";
							tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("acctCur"));
							tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("acctName");
							tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balance").replaceAll(",", "");
							tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("curBal").replaceAll(",", "");
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpRetInfo;
					}
					retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
				} else {
					retAts.writeRespCode("-1");
					retAts.writeRespInfo(tmpSbMsg.toString());
				}
			} else {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo(tmpStrRet[1]);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}

		retAts.writeEnd();
		return retAts;
	}

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}